﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using Peninsula.Utility.Extensions;
using System.Web;
using System.Web.Mvc;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Web.Mvc.Html;

namespace Peninsula.Site.Data.Helper
{
    public static class HtmlHelperExtensions
    {
        /// <summary>
        /// html拓展 :用于获取枚举类型的中文描述
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="value">枚举值</param>
        /// <returns></returns>
        public static HtmlString EnumDescription(this HtmlHelper helper, Enum value)
        {
            Type enumType = value.GetType();
            string name = Enum.GetName(enumType, value);

            if(name != null)
            {
                FieldInfo fieldInfo = enumType.GetField(name);
                if(fieldInfo != null)
                {
                    DescriptionAttribute attr = Attribute.GetCustomAttribute(fieldInfo,
                        typeof(DescriptionAttribute), false) as DescriptionAttribute;
                    if(attr != null)
                    {
                        name = attr.Description;
                    }
                }
            }
            return new HtmlString(name);
        }

        /// <summary>
        /// html拓展 :用于获取多个枚举值的中文描述
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="enumType">枚举类型</param>
        /// <param name="namesStr">枚举字段的字符串</param>
        /// <param name="split">描述的分隔符</param>
        /// <returns></returns>
        public static HtmlString EnumsDescription(this HtmlHelper helper, Type enumType, string namesStr, string split = "|")
        {
            var items = SelectListItemsHelper.FromEnum(enumType);
            var names = namesStr.ToList();
            var texts = items.Where(i => names.Contains(i.Value)).Select(i => i.Text).ToList();
            return new HtmlString(string.Join(split, texts));
        }

        /// <summary>
        /// 枚举下拉框
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="enumType"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString EnumDesriptionDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes, string extraFirstItem = null, string[] excludeValues = null)
        {
            IEnumerable<SelectListItem> list = SelectListItemsHelper.FromEnum(default(TProperty).GetType(), extraFirstItem: extraFirstItem);
            if (excludeValues.IsNotEmpty())
            {
                list = list.Where(t => !excludeValues.Contains(t.Value));
            }
            return htmlHelper.DropDownListFor(expression, list, htmlAttributes);
        }

        /// <summary>
        /// 可以为null的下拉选项
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="selectList"></param>
        /// <param name="htmlAttributes"></param>
        /// <param name="firstNullText"></param>
        /// <returns></returns>
        public static MvcHtmlString DropDownAllowNullListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IList<SelectListItem> selectList, string firstNullText, object htmlAttributes)
        {
            if (selectList.IsNull())
            {
                selectList = new List<SelectListItem>();
            }

            if (firstNullText.IsNotNullAndNotWhiteSpace())
            {
                selectList.Insert(0, new SelectListItem() { Text = firstNullText, Value = "" });
            }
            return htmlHelper.DropDownListFor(expression, selectList, htmlAttributes);
        }
        
    }
}
